package pytest

import scaladuct.{PhaseMethodInterceptor, ConfigPage, ConfigPageGateway, GatewayWithHints}
import scaladuct.validation.{ConstraintViolationException, ValidatingGateway}
import javax.validation.constraints.Min
import javax.validation.{ValidatorFactory, Validation, ConstraintViolation}
import scaladuct.memento.{StoredMementoConfig, DummyMementoStorage, MementoPlayingGateway, MementoRecordingGateway}
import java.util.{Collections, Locale}

/**
 * <p>
 * Created by IntelliJ IDEA.
 * User: zslajchrt
 * Date: Jan 16, 2011
 * Time: 6:55:04 PM
 */

class GatewayA(firstPage: ConfigPage[Any], val locale: Locale,
               val validationException: ConstraintViolationException,
               val contextName: String)
        extends ConfigPageGateway[Any](firstPage)
          with GatewayWithHints[Any]
          with MementoRecordingGateway[Any]
          with MementoPlayingGateway[Any]
          with ValidatingGateway[Any] {
  val mementoStorage = DummyMementoStorage

  protected def saveMemento(storedMementoConfig: StoredMementoConfig) = null
}

class GatewayAFactory {
  def newGateway(starterClass: Class[Any]) = {
    val interceptor = PhaseMethodInterceptor[Any](starterClass)
    val gw = new GatewayA(interceptor.page, Locale.ENGLISH, generateConstraintException(), "ContextA")
    val cookieBean = new scaladuct.CompositeCookieBean(gw.getCookie)
    gw
  }

  private def generateConstraintException() = {
    class X {
      @Min(10)
      private var parameter0: Int = 1
    }
    val x = new X()
    val validatorFactory: ValidatorFactory = Validation.buildDefaultValidatorFactory();
    val validator: javax.validation.Validator = validatorFactory.getValidator();
    //val constraintViolationSet: java.util.Set[ConstraintViolation[AnyRef]] = validator.validate(x);
    val constraintViolationSet: java.util.Set[ConstraintViolation[AnyRef]] = Collections.emptySet[ConstraintViolation[AnyRef]];
    new ConstraintViolationException(constraintViolationSet)

  }
}
